package jmvcclab.Controller;

import jmvcclab.Model.MainData;
import jmvcclab.Model.OperationValues;

public final class Calc {

    OperationValues OpValues;
    MainData m;
    public Calc(MainData model) {
        init(model);
    }

    public void init(MainData model) {
        this.m = model;
        int i = 0;
        m.Query = Integer.toString(i);
        QuerryListener ql = new QuerryListener(m);
    }
    public double getResult(){
        return getAnswer(getResult(m.SortedValues, m.SortedOperations));
    }

    public int getAnswer(int result[]) {
        int Answer = 0;
        for (int i = 0; i < result.length; i++) {
            Answer += result[i];
        }
        return Answer;
    }

    public int[] getResult(int SortedValues[], int SortedOperations[]) {
        int result[] = null;
        for (int i = 0; i < SortedOperations.length-1; i++) {
            result[i] = getOperationResult(SortedValues[i], SortedValues[i + 1],
                    SortedOperations[i]);
        }
        return result;
    }

    private int getOperationResult(int LeftValue, int RightValue, int Operation) {
        int result;
        switch (OpValues) {
            case PLUS:
                result = LeftValue + RightValue;
            case MINUS:
                result = LeftValue - RightValue;
            case MULT:
                result = LeftValue * RightValue;
            case UNMULT:
                result = LeftValue / RightValue;
            default:
                result = 0;
        }
        return result;
    }
}
